package com.yx.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yx.data.Result;
import com.yx.entity.SysUser;
import com.yx.mapper.SysUserMapper;
import com.yx.page.Page;
import com.yx.service.SysUserService;
import com.yx.util.ShiroUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @Description: 用户
 * @Auther: zcz
 * @create 2020/4/16 11:51
 */
@Service
@Slf4j
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public Result update(SysUser sysUser) {
        return null;
    }

    @Override
    public Result delete(String id) {
        return null;
    }

    @Override
    public Result getList(Page page, SysUser sysUser) {
        log.info("-------------");
        //只有紧跟在PageHelper.startPage方法后的第一个Mybatis的查询（Select）方法会被分页。
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<SysUser> list = sysUserMapper.getList(sysUser);
        log.info("-----list--------:"+list.size());
        PageInfo<SysUser> pageInfo = new PageInfo<>(list);
        return Result.data(pageInfo);
    }
    @Override
    public Result insert(SysUser sysUser) {
        String salt = ShiroUtil.createSalt();
        sysUser.setSalt(salt);
        String pass = null == sysUser.getPassword() ? "123456" : sysUser.getPassword();
        sysUser.setPassword(ShiroUtil.createPassWord(pass,salt));
        sysUserMapper.insert(sysUser);
        return Result.ok("用户添加成功");
    }
    @Override
    public Result getInfo(String id) {
        SysUser info = sysUserMapper.getInfo(id);
        return Result.ok(info);
    }
}
